<template>
  <div class="app-container">
    <el-row :gutter="20">
      
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="名称" prop="supplier">
            <el-input
              v-model="queryParams.supplier"
              placeholder="请输入名称查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="telephone">
              <el-input
                v-model="queryParams.telephone"
                placeholder="请输入手机号查找"
                clearable
                size="small"
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
          </el-form-item>
          <el-form-item label="联系电话" prop="phoneNum">
              <el-input
                v-model="queryParams.phoneNum"
                placeholder="请输入联系电话查找"
                clearable
                size="small"
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
          </el-form-item>
          <el-form-item label="类型" prop="type">
              <el-input
                v-model="queryParams.type"
                placeholder="请输入类型"
                clearable
                size="small"
                style="width: 240px"
                @keyup.enter.native="handleQuery"
                v-if="false"
              />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['basic:supplier:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['basic:supplier:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['basic:supplier:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['basic:supplier:import']"
            >导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['basic:supplier:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column type="index" width="50" align="center" /> 
          <el-table-column label="名称" align="center" key="supplier" prop="supplier" v-if="columns[0].visible" :show-overflow-tooltip="true" />
          <el-table-column label="联系人" align="center" key="contacts" prop="contacts" v-if="columns[1].visible" />
          <el-table-column label="手机号码" align="center" key="telephone" prop="telephone" v-if="columns[2].visible" />
          <el-table-column label="联系电话" align="center" key="phoneNum" prop="phoneNum" v-if="columns[3].visible" />
          <el-table-column label="期初应付" align="center" key="beginNeedPay" prop="beginNeedPay" v-if="columns[4].visible" />
          <el-table-column label="期末应付" align="center" key="allNeedPay" prop="allNeedPay" v-if="columns[5].visible" />
          <el-table-column label="税率(%)" align="center" key="taxRate" prop="taxRate" v-if="columns[6].visible" />
          <el-table-column label="状态" align="center" key="status" v-if="columns[7].visible">
          <el-table-column label="类型" align="center" key="type" prop="type" v-show="false" v-if="columns[8].visible" />
            <template slot-scope="scope">
              <el-tag v-if="scope.row.enabled==1">启用</el-tag>
              <el-tag v-if="scope.row.enabled==0">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['basic:supplier:edit']"
              >修改</el-button>       
                    
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="supplier">
              <el-input v-model="form.supplier" placeholder="请输入名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phoneNum">
              <el-input v-model="form.phoneNum" placeholder="请输入联系电话" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="期初应付" prop="beginNeedPay">
              <el-input v-model="form.beginNeedPay" placeholder="请输入期初应付" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="期末应付" prop="allNeedPay">
              <el-input v-model="form.allNeedPay" placeholder="请输入期末应付" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="税率(%)" prop="taxRate">
              <el-input v-model="form.taxRate" placeholder="请输入税率(%)" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="false">
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-input v-model="form.type" placeholder="客户" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="enabled">
               <el-radio v-model="form.enabled" :label="1">启用</el-radio>
               <el-radio v-model="form.enabled" :label="0">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-file-module v-bind:upload="upload"></import-file-module>
  </div>
</template>
<script>
import {listSupplier,getSupplier, delSupplier, addSupplier, updateSupplier } from "@/api/basic/supplier";
import  importFile  from '@/components/import/index';
import { getToken } from "@/utils/auth";
export default {
  name: "index",
  components:{
    "import-file-module":importFile
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 用户导入参数
      upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/basic/supplier/importData",
          // 模板下载地址
          downloadTemplate: "/basic/supplier/importTemplate"
      },
     
      // 是否显示弹出层
      open: false,
 
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
     
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        supplier: undefined,
        telephone: undefined,
        phoneNum: undefined,
        type: "客户"
      },
      // 列信息
      columns: [
        { key: 0, label: `名称`, visible: true },
        { key: 1, label: `联系人`, visible: true },
        { key: 2, label: `手机号码`, visible: true },
        { key: 3, label: `联系电话`, visible: true },
        { key: 4, label: `期初应付`, visible: true },
        { key: 5, label: `期末应付`, visible: true },
        { key: 6, label: `税率(%)`, visible: true },
        { key: 7, label: `状态`, visible: true },
        { key: 8, label: `类型`, visible: false }
      ],
      // 表单校验
      rules: {
        supplier: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        contacts: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        telephone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
        
      }
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listSupplier(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.data.list;
          this.total = response.data.totalCount;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplier: undefined,
        contacts: undefined,
        telephone: undefined,
        phoneNum: undefined,
        beginNeedPay: undefined,
        allNeedPay: undefined,
        taxRate: undefined,
        enabled: 1,
        type: "客户"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
   
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSupplier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
      
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplier(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSupplier(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
     /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/supplier/export', {
        ...this.queryParams
      }, `basic_supplier_${new Date().getTime()}.xlsx`)
    },
    
  }
}
</script>